<template>
  <div v-loading="loading">
    <a-card>
      <title-name title="用印申请" />
      <a-form-model
        ref="ruleForm"
        :model="form5"
        :rules="form5Rules"
      >
        <a-row>
          <a-col span="24">
            <a-form-model-item label="合同名称">
              <BaseInput
                v-model="baseForm.contractName"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="24">
            <a-form-model-item label="相对方">
              <BaseInput
                v-model="baseForm.otherParty"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="申请单位">
              <BaseInput
                v-model="baseForm.branchComName"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="合同额（万元）">
              <BaseInput
                v-model="baseForm.amount"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="合同类型">
              <BaseInput
                v-model="baseForm.contractTypeStr"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="合同编号">
              <BaseInput
                v-model="form5.contractCode"
                :disabled="isView"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item
              label="合同份数"
            >
              <BaseInputNumber
                v-model="form5.contractQuantity"
                placeholder="请输入"
                :precision="0"
                :min="1"
                :disabled="isView"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="项目经理">
              <BaseInput
                v-model="baseForm.pmEmployeeName"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="项目经理联系电话">
              <BaseInput
                v-model="baseForm.pmEmployeePhone"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="24">
            <a-form-model-item
              label="特别说明"
              prop="specialInstructions"
            >
              <BaseTextarea
                v-model="form5.specialInstructions"
                placeholder="请输入"
                :disabled="isView"
                :auto-size="{ minRows: 3, maxRows: 5 }"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item
              label="本合同是否按规定流程经过评审通过"
              prop="review"
            >
              <a-select
                v-model="form5.review"
                :disabled="isView"
                placeholder="请选择本合同是否按规定流程经过评审通过"
              >
                <a-select-option
                  :key="1"
                  :value="1"
                >
                  是
                </a-select-option>
                <a-select-option
                  :key="0"
                  :value="0"
                >
                  否
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item
              label="本合同审核意见处理情况"
              prop="auditHandle"
            >
              <a-select
                v-model="form5.auditHandle"
                :disabled="isView"
                placeholder="请选择本合同审核意见处理情况"
              >
                <a-select-option
                  :key="1"
                  :value="1"
                >
                  无审核意见
                </a-select-option>
                <a-select-option
                  :key="2"
                  :value="2"
                >
                  已按审核意见采纳
                </a-select-option>
                <a-select-option
                  :key="3"
                  :value="3"
                >
                  未采纳
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col span="24">
            <a-form-model-item
              label="本合同审核意见处理情况"
              prop="handleNote"
            >
              <BaseTextarea
                v-model="form5.handleNote"
                placeholder="请填写本合同审核意见处理情况"
                :disabled="isView"
                :max-length="1000"
                :auto-size="{ minRows: 3, maxRows: 5 }"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="经办责任人">
              <a-input
                v-model="form5.agentName"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="经办责任人联系电话">
              <BaseInput
                v-model="form5.agentEmployeePhone"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="申请时间">
              <BaseInput
                v-model="form5.applyTime"
                disabled
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>
    <a-card>
      <title-name title="附件" />
      <u-table
        :is-show="!form5.sealApply || form5.sealApply === 'DRAFT'"
        business-code="LCM08"
        :is-upload="false"
        :file-arrys.sync="form5.fileInfos"
        :upload-arrys="uploadArrys"
      />
    </a-card>
    <div class="operation page-btn-right-top">
    <!-- <div class="operation mb20p mr20p tar"> -->
      <template v-if="form5.sealApply !== 'SUBMITTED'">
        <a-button
          type="primary"
          class="mr20p"
          @click="submit('DRAFT')"
        >
          保存草稿
        </a-button>
        <a-button
          type="primary"
          class="mr20p"
          @click="confirmSubmit"
        >
          提交
        </a-button>
      </template>
      <a-button @click="$close()">
        关闭
      </a-button>
    </div>
      <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </div>
</template>

<script>
import totalPackageContractApi from '@/api/legal/branchContract/totalPackageContract'
import { getInfoByStaffId } from '@/api/commons'
import moment from 'moment'

export default {
    name: 'UsePrintApply',
    props: {
        baseForm: {
            type: Object,
            default() {
                return {}
            }
        }
    },
    data() {
        return {
            loading: false,
            form5: {
                agentName: void 0,
                applicantName: void 0,
                applyTime: void 0
            },
            majorRisks: null,
            form5Rules: {
                contractQuantity: [{ required: true, message: '请填写合同份数', trigger: 'change' }],
                specialInstructions: [{ required: true, message: '请填写特别说明', trigger: 'blur' }],
                review: [{ required: true, message: '请选择本合同是否按规定流程经过评审通过', trigger: 'change' }],
                auditHandle: [{ required: true, message: '请选择本合同审核意见处理情况', trigger: 'change' }]
            },
            postOptions: [],
            uploadArrys: [],
            //获取流程配置值
            processConfig: null,
           //点击提交发送流程配置值
           setProcessConfig: null,
           businessId: null
        }
    },
    computed: {
        // 是否为查看；当授权申请状态存在且不为草稿状态，只能查看表单
        isView() {
            return this.form5.sealApply && this.form5.sealApply !== 'DRAFT' && this.form5.sealApply !== 'REJECTED'
        }
    },
    async created() {
        await this.getData()
        if (!this.form5.baseId) {
            this.form5.agentId = this.$userInfo()?.id
            this.form5.agentName = this.$userInfo()?.name
            this.form5.applyTime = this.$dateFormat(new Date(), 'D')
            this.form5.agentEmployeePhone = this.$userInfo()?.employee?.phone
        }
        //获取通过获取流程配置值
       this.processConfig = await this.$getConfigFn('legal_general_applyforseal_new')
    },
    methods: {
        async confirmSubmit() {
            this.setProcessConfig = null; 
            if (!(await this.submit('DRAFT', true))) {
                return
            }
            // this.$confirm({
            //   title: '是否有重大风险？',
            //   content: (h) => {
            //     return (
            //       <a-select style={{ width: '100px' }} onChange={(e) => (this.majorRisks = e)} placeholder="请选择">
            //         <a-select-option value="1">是</a-select-option>
            //         <a-select-option value="0">否</a-select-option>
            //       </a-select>
            //     )
            //   },
            //   onOk: async () => {
            //     if (this.majorRisks) {
            this.loading = true
            let res = await totalPackageContractApi.generalSubmit({
                auditStatusEnum: 'SUBMITTED',
                baseId: this.baseForm.baseId,
                // majorRisks: this.majorRisks,
                submitCode: '1122'
            })
            this.loading = false
            if (res.code === 200) {
                this.$noticeSuccess('提交成功')
                   //提交时，返回id
                this.businessId = res.data?.businessId;
                this.setProcessConfig = this.processConfig;
                setTimeout(() => {
                    //this.$close()
                    return Promise.resolve()
                }, this.TIMEOUT_DELAY)
            } else {
                return Promise.reject()
            }
            //     } else {
            //       this.$noticeWarning('请选择是否有重大风险')
            //       return Promise.reject()
            //     }
            //   }
            // })
        },
        async getApplicantName(row) {
            this.form5.applicantName = row.name
            this.form5.applicantEmployeeId = row.userId
        },
        async getData() {
            if (this.baseForm.baseId) {
                this.loading = true
                let res = await totalPackageContractApi.findApplyForSealByBaseId(
                    {},
                    {
                        edit: true,
                        baseId: this.baseForm.baseId
                    }
                )
                if (res.code === 200) {
                    if (res.data.fileInfos?.length > 0) {
                        this.uploadArrys = res.data.fileInfos
                    }
                    res.data.applyTime = moment(Number(res.data.applyTime)).format('YYYY-MM-DD')
                  if (res.data.review === null){
                    res.data.review = undefined
                  }
                    res.data.auditHandle = res.data.auditHandle || undefined
                    this.form5 = res.data
                    if (res.data.agentEmployeeId) {
                        let res2 = await getInfoByStaffId(res.data.agentEmployeeId)
                        if (res2.data) {
                            this.postOptions = res2.data.posts
                        }
                    }
                }
                this.loading = false
            }
        },
        async submit(status, isSubmit = false) {
            return new Promise((resolve) => {
                this.$refs.ruleForm.validate(async (valid, object) => {
                    if (valid) {
                        this.loading = true
                        let form = this.$clone(this.form5)
                        let post = this.postOptions.find((v) => v.id === form.trusteePostId)
                        post && (form.trusteePostName = post.name)
                        form.baseId = this.baseForm.baseId
                        form.sealApply = status
                        form.applyTime = moment(form.applyTime).format('x')
                        let res = await totalPackageContractApi.updateApplyForSeal(form)
                        if (res.code === 200) {
                            if (isSubmit) {
                                resolve(true)
                            } else {
                                this.$noticeSuccess('保存成功')
                                this.$close()
                            }
                        }
                        this.loading = false
                        resolve(false)
                    }else {
                      this.$message.warning("请填写完所有必填项信息")
                    }
                })
            })
        }
    }
}
</script>

<style lang="less" scoped>
/deep/.ant-form-item {
    .ant-form-item-label {
        width: 150px;
    }
    .ant-form-item-control-wrapper {
        width: calc(100% - 150px);
    }
}
</style>
